const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require("webpack");
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

//定义了一些文件夹的路径
const ROOT_PATH = path.resolve(__dirname);
const SRC_PATH = path.resolve(ROOT_PATH, 'src');
const BUILD_PATH = path.resolve(ROOT_PATH, 'build');

// 加载File System读写模块  
const COPYRIGHT = require('fs').readFileSync("./copyright.txt","utf-8");  

const config = {
	devtool: 'eval-source-map',
	entry: path.resolve(SRC_PATH,"entry.js"),
	output:{
		filename:"js/[name]-bundle.js",
		// publicPath:"http://www.nicekun.com",
		path:BUILD_PATH
	},
	module:{
		rules:[
		{
			test:/\.css$/,
			use:"style-loader!css-loader!postcss-loader"
		},

		{
			test:/\.js$/,
			exclude:path.resolve(__dirname,'node_modules'),
			use:{
				loader:'babel-loader',
				options:{
					cacheDirectory:false,
					"presets": ["env"]
				}
			}
		},

		{
			test: /\.scss$/,
			use:  ExtractTextPlugin.extract({
				fallback: "style-loader",
				use:"css-loader?modules=true!postcss-loader!sass-loader"
			})
		},

		{test:/\.(png|jpg|gif|svg$)/,loader:"url-loader?limit=200&name=images/[name]-[hash:8].[ext]"}

		]
	},
	plugins:[
		// new webpack.optimize.UglifyJsPlugin({
		// 	compress: {
		// 	    warnings: true
		// 	},
		// 	except: ['$super', '$', 'exports', 'require']
		// }),
		new CleanWebpackPlugin(['dist']),
		
		new HtmlWebpackPlugin({
			title:"Hello",
			filename:"index.html",
			inject:"body",
			template:"./src/mypage.ejs",
			// minify:{
		 //    	removeComments:false,
		 //    	collapseWhitespace:true
		 //    },
		    hash: true
		}),

		new webpack.BannerPlugin(COPYRIGHT),
		new ExtractTextPlugin("style/[name].css")
	],

	devServer: {
	    contentBase: "./build",//本地服务器所加载的页面所在的目录
	    historyApiFallback: true,//不跳转
	    inline: true//实时刷新
  	} 
}

module.exports = config;